package com.xxx.seckill.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;

import static com.xxx.seckill.generator.YmlUtils.getString;

/**
 * @Author: xxx
 * @Date: 2022/11/26 22:37
 * @Description: 3.5.+的生成器
 */
public class AutoGenerator {
    
    public static void main(String[] args) {

        Yaml yaml = new Yaml();
        InputStream in = YmlUtils.class.getResourceAsStream("/application.yml");
        LinkedHashMap<String, Object> sourceMap = (LinkedHashMap<String, Object>) yaml.load(in);
        String driverClassName = getString(sourceMap, "spring.datasource.driver-class-name");
        String url = getString(sourceMap, "spring.datasource.url");
        String username = getString(sourceMap, "spring.datasource.username");
        String password = getString(sourceMap, "spring.datasource.password");
        String directoryName = getString(sourceMap, "tensquare.directoryName");
        String packageName = getString(sourceMap, "tensquare.packageName");
        //获取项目路径
        String projectPath = System.getProperty("user.dir");


        
        FastAutoGenerator.create(url, username, password)
                //全局配置
                .globalConfig((scanner, builder) -> {
                    builder.author(scanner.apply("请输入作者名？"))
                            .outputDir(projectPath + directoryName+"/src/main/java")//输出路径
                            //.enableSwagger()//开启swagger3
                            .fileOverride()//覆盖文件
                            .disableOpenDir();//不打开文件夹
                })
                //包名配置
                .packageConfig((scanner, builder) -> {
                    builder.parent(packageName)
                            //.moduleName("blog")
                            .service("service")
                            .serviceImpl("service.impl")
                            .controller("controller")
                            .entity("pojo")
                            .mapper("mapper")
                            //自定义输出路径，mapper.xml生成到resources目录下
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, projectPath + directoryName+ "/src/main/resources/mapper"));
                })
                //策略配置
                .strategyConfig((scanner, builder) -> {
                    builder.addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入 all")))
                            //.addTablePrefix("t_")//表前缀
                            .serviceBuilder().formatServiceFileName("%sService")//去掉Service的 "I" 前缀
                            .controllerBuilder().enableRestStyle()//restful开启
                            .enableHyphenStyle()//url改变 例如：index_id_1
                            .entityBuilder()
                            .enableTableFieldAnnotation()//开启生成实体时生成字段注解，默认值:false
                            .naming(NamingStrategy.underline_to_camel)//数据库表映射到实体的命名策略，默认下划线转驼峰命名:NamingStrategy.underline_to_camel
                            .columnNaming(NamingStrategy.underline_to_camel)//数据库表字段映射到实体属性的命名策略，默认为 null，未指定按照 naming 执行
                            //.idType(IdType.ASSIGN_ID)//全局主键类型
                            .enableLombok();//开启lombok
                    
                })
                // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .templateEngine(new FreemarkerTemplateEngine())
                //执行
                .execute();

    }

    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }
}
